package craky.keeper.allpay;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import org.hibernate.criterion.Criterion;

import craky.chart.Bar3DChart;
import craky.chart.Chart3D;
import craky.chart.Pie3DChart;
import craky.componentc.JCCheckBox;
import craky.componentc.JCCheckBoxList;
import craky.keeper.KeeperApp;
import craky.keeper.KeeperConst;
import craky.keeper.KeeperMgr;
import craky.keeper.KeeperPane;

public class AllPayPane extends KeeperPane {
	private static final long serialVersionUID = -381623737074535966L;
	private static final DateFormat DATE_FORMAT = new SimpleDateFormat(
			"yyyy-MM");
	public static final String TYPE = "全部";
	private Map<String, Number> pieMap;
	private Map<String, Number> barMap;
	private Chart3D pieChart;
	private Chart3D barChart;

	public AllPayPane(KeeperApp keeper, KeeperMgr mgr) {
		super(keeper, mgr);
		setImageOnly(true);
	}

	@Override
	protected JComponent createRight() {
		this.right = new JPanel();
		this.pieChart = new Pie3DChart(
				this.pieMap = new HashMap<String, Number>(), 10, "消费类别统计");
		this.barChart = new Bar3DChart(
				this.barMap = new HashMap<String, Number>(), 10, 10, "消费日期统计");
		this.right.setPreferredSize(new Dimension(400, -1));
		this.right.setBorder(new CompoundBorder(new LineBorder(new Color(84,
				165, 213)), new EmptyBorder(5, 3, 5, 3)));
		this.right.setLayout(null);
		this.right.setBackground(Color.WHITE);
		this.right.setOpaque(true);
		((Pie3DChart) this.pieChart).setDataFormat(KeeperConst.AMOUNT_FORMAT);
		this.pieChart.setMaximumSize(new Dimension(1000, 300));
		((Bar3DChart) this.barChart).setFormat(KeeperConst.AMOUNT_FORMAT);
		this.right.add(this.pieChart);
		this.right.add(this.barChart);
		this.right.setVisible(false);
		this.right.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				AllPayPane.this.layoutStat();
			}
		});
		return this.right;
	}

	private void layoutStat() {
		Insets insets = this.right.getInsets();
		int width = this.right.getWidth() - insets.left - insets.right;
		int height = this.right.getHeight() - insets.top - insets.bottom;
		int maxPieHeight = this.pieChart.getMaximumSize().height;
		int pieHeight = 0;
		int barHeight = 0;

		if (!this.barChart.isVisible()) {
			pieHeight = Math.min(height, maxPieHeight);
		} else {
			pieHeight = Math.min(maxPieHeight, (height - 20) / 2);
			barHeight = height - pieHeight - 20;
		}

		this.pieChart.setBounds(insets.left, insets.top, width, pieHeight);
		this.barChart.setBounds(insets.left, insets.top + pieHeight + 20,
				width, barHeight);
	}

	@Override
	protected Criterion[] createCriterions(String type) {
		return super.createCriterions(null);
	}

	@Override
	protected void resetRowValues(AllPay pay) {
		super.resetRowValues(pay);
		pay.setType(String.valueOf(this.cpType.getSelectedItem()));
	}

	@Override
	protected List<AllPay> filterResult(List<AllPay> list) {
		JCCheckBoxList checkList = this.cpFType.getCheckedList();

		if ((!checkList.isSelectedAll()) && (!checkList.isSelectedEmpty())) {
			@SuppressWarnings("rawtypes")
			List selTypes = this.cpFType.getSelectedItems();

			for (int i = list.size() - 1; i >= 0; i--) {
				if (selTypes.contains(list.get(i).getType()))
					continue;
				list.remove(i);
			}

		}

		return list;
	}

	@Override
	protected JCCheckBox createRightSplitButton() {
		this.btnShowRight = new JCCheckBox();
		this.btnShowRight.setPreferredSize(new Dimension(6, -1));
		this.btnShowRight.setIcon(this.emptyIcon);
		this.btnShowRight.setSelectedIcon(this.emptyIcon);
		this.btnShowRight.setRolloverIcon(this.splitRight);
		this.btnShowRight.setRolloverSelectedIcon(this.splitLeft);
		this.btnShowRight.setPressedIcon(this.splitRight);
		this.btnShowRight.setPressedSelectedIcon(this.splitLeft);
		this.btnShowRight.setFocusable(false);
		this.btnShowRight.setToolTipText("显示统计");
		this.btnShowRight.addItemListener(this.splitListener);
		return this.btnShowRight;
	}

	@Override
	public void updateCategory() {
		resetTypes();
	}

	@Override
	public void refreshStat() {
		if (!isShowStat()) {
			return;
		}

		this.pieMap.clear();
		this.barMap.clear();

		for (AllPay pay : this.data) {
			float amount = pay.getAmount();
			String type = pay.getType();
			String date = DATE_FORMAT.format(pay.getDate());

			if (this.pieMap.containsKey(type)) {
				this.pieMap.put(
						type,
						Double.valueOf(this.pieMap.get(type).doubleValue()
								+ amount));
			} else {
				this.pieMap.put(type, Float.valueOf(amount));
			}

			if (this.barMap.containsKey(date)) {
				this.barMap.put(
						date,
						Double.valueOf(this.barMap.get(date).doubleValue()
								+ amount));
			} else {
				this.barMap.put(date, Float.valueOf(amount));
			}
		}

		this.pieChart.setDatasAndRepaint(this.pieMap);
		this.barChart.setDatasAndRepaint(this.barMap);
	}

	@Override
	public String getType() {
		return "全部";
	}
}